package com.ems.remote.components;

import com.ems.bo.FrameBo;
import com.ems.remote.IStrategy;
import com.ems.service.IDeviceService;
import com.ems.util.EmsPointUtil;
import com.ems.util.HexUtil;
import com.ems.util.RedisUtils;
import com.ems.vo.DeviceVo;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class PcsResponseStrategy implements IStrategy {

    @Resource
    private IDeviceService iDeviceService;

    @Override
    public void action (FrameBo frameBo, ChannelHandlerContext channelHandlerContext) {
        //010101013501，去掉所有的有效数据
        log.info ( "PCS遥控响应，设备地址->{}, 数据->{}", frameBo.getDeviceAddress (), frameBo.getData () );
        //总包
        int countFrame = HexUtil.hexToDecimal ( frameBo.getData (), 0, 1 );
        int currentFrame = HexUtil.hexToDecimal ( frameBo.getData (), 1, 1 );
        int number = HexUtil.hexToDecimal ( frameBo.getData (), 2, 1 ); // 测点的数量
        //测点Id
        int point = HexUtil.hexToDecimal ( frameBo.getData (), 3, 2 );
        //响应值
        int value = HexUtil.hexToDecimal ( frameBo.getData (), 5, 1 );
        log.info ( "总帧->{},当前帧->{},数量->{},测点->{},响应值->{}", countFrame, currentFrame, number, point, value );
        DeviceVo deviceVo = new DeviceVo ();
        deviceVo.setDeviceAddress ( String.valueOf ( HexUtil.hexToDecimal ( frameBo.getDeviceAddress (), 0, 2 ) ) );
        if ( EmsPointUtil.PCS_POWER_ON_OFF_COMM == point ) {
            // 开关机指令
            if ( value == 1 ) {
                // 开关机设置成功
                String key = deviceVo.getDeviceAddress ().concat ( "#connect" );
                String connectStatus = (String) RedisUtils.get ( key );
                log.info ( "key->{}, value->{}", key, connectStatus );
                deviceVo.setConnectStatus ( Integer.parseInt ( connectStatus ) );
                iDeviceService.updateSingle ( deviceVo );
                RedisUtils.delete ( key );
            }
        } else if ( EmsPointUtil.PCS_OFF_GRID_MODEL_SET == point ) {
            //并离网模式设置
            if ( value == 1 ) {
                // 开关机设置成功
                String key = deviceVo.getDeviceAddress ().concat ( "#offGrid" );
                String offGridStatus = (String) RedisUtils.get ( key );
                deviceVo.setOffGridStatus ( Integer.parseInt ( offGridStatus ) );
                iDeviceService.updateSingle ( deviceVo );
                RedisUtils.delete ( key );
            }
        } else if ( EmsPointUtil.PCS_DIS_CHARGE_POWER == point ) {
            if ( value == 1 ) {
                //下设有功充电/放电功率
                log.info ( "PCS下设有功充电/放电功率执行状态->{}", value );
                String key = deviceVo.getDeviceAddress ().concat ( "#power" );
                String powerValue = (String) RedisUtils.get ( key );
                deviceVo.setChargeDisPower ( powerValue );
                iDeviceService.updateSingle ( deviceVo );
                RedisUtils.delete ( key );
            }
        } else if ( EmsPointUtil.PCS_FAIL_RESET_COMM == point ) {
            //故障复位
            log.info ( "PCS故障复位设置响应->{}", value );
        }
    }
}
